from PySide2.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QMessageBox, QFormLayout, QLineEdit, QPushButton, QLabel, QTextEdit
from tool import util
from custom.ConsoleWidget import ConsoleWidget
from scrapyworker import ScrapyWorker

CollectConfigKey = "CollectConfigKey"

class TabCollect(QWidget):

    def __init__(self):
        super(TabCollect, self).__init__()
        self.is_collect = False
        self.collect_type = ""
        self.setupUi()
        self.setupData()
        self.scrapy_worker = ScrapyWorker(self)
        self.scrapy_worker.logChanged.connect(self.insert_log)
        self.scrapy_worker.started.connect(self.started_scrapy)
        self.scrapy_worker.finished.connect(self.finished_scrapy)

    def setupUi(self):
        self.layout = QVBoxLayout(self)

        self.titleLabel = QLabel("腾讯视频")
        self.layout.addWidget(self.titleLabel)

        self.txLayout = QHBoxLayout()
        self.layout.addLayout(self.txLayout)

        self.formLayout = QFormLayout()

        self.searchEdit = QTextEdit()
        self.searchEdit.setMaximumHeight(40)
        self.searchEdit.textChanged.connect(self.searchChanged)
        self.pageEdit = QLineEdit()
        self.pageEdit.textChanged.connect(self.pageChanged)
        self.creatorPageEdit = QLineEdit()
        self.creatorPageEdit.textChanged.connect(self.creatorPageChanged)
        self.creatorTimeEdit = QLineEdit()
        self.creatorTimeEdit.textChanged.connect(self.creatorTimeChanged)

        self.formLayout.addRow("采集内容", self.searchEdit)
        self.formLayout.addRow("采集页码", self.pageEdit)
        self.formLayout.addRow("采集作者页码", self.creatorPageEdit)
        self.formLayout.addRow("距今天作者", self.creatorTimeEdit)

        self.searchEdit.setPlaceholderText("分号(`,`)隔开")
        self.pageEdit.setPlaceholderText("3")
        self.creatorPageEdit.setPlaceholderText("1")
        self.creatorTimeEdit.setPlaceholderText("0=今天，-1=昨天, >0=所有")

        self.buttonLayout = QVBoxLayout()
        self.button1 = QPushButton("常规采集")
        self.button1.clicked.connect(self.onClick)

        self.button2 = QPushButton("搜索采集")
        self.button2.clicked.connect(self.onClickSearch)

        self.button3 = QPushButton("采集作者视频")
        self.button3.clicked.connect(self.onClickCreator)
        
        self.buttonLayout.addWidget(self.button1)
        self.buttonLayout.addWidget(self.button2)
        self.buttonLayout.addWidget(self.button3)

        self.txLayout.addLayout(self.formLayout)
        self.txLayout.addLayout(self.buttonLayout)
        self.txLayout.addStretch(1)

        self.consoleWidget = ConsoleWidget()
        self.layout.addWidget(self.consoleWidget)
        self.layout.setStretchFactor(self.consoleWidget, 1)


    def setupData(self):
        self.configDic = util.get_collect_config()
        self.searchEdit.setText(self.configDic["keyword"])
        self.pageEdit.setText(self.configDic["page"])
        self.creatorPageEdit.setText(self.configDic["creatorPage"])
        self.creatorTimeEdit.setText(self.configDic["creatorTime"])

    # TODO 只能执行一个采集
    def onClick(self):
        self.onCollect("common")

    def onClickSearch(self):
        self.onCollect("txv")

    def onClickCreator(self):
        self.onCollect("txcreator")
    
    def onCollect(self, type):
        if self.is_collect:
            QMessageBox.warning(self, '', "采集中...", QMessageBox.Yes)
            return
        self.is_collect = True
        self.collect_type = type
        if type == "common":
            self.scrapy_worker.run("txv")
        else:
            self.scrapy_worker.run(type)

    def searchChanged(self):
        text = self.searchEdit.toPlainText()
        self.configDic["keyword"] = text
        util.set_collect_config(self.configDic)

    def pageChanged(self):
        text = self.pageEdit.text()
        self.configDic["page"] = text
        util.set_collect_config(self.configDic)

    def creatorPageChanged(self):
        text = self.creatorPageEdit.text()
        self.configDic["creatorPage"] = text
        util.set_collect_config(self.configDic)

    def creatorTimeChanged(self):
        text = self.creatorTimeEdit.text()
        self.configDic["creatorTime"] = text
        util.set_collect_config(self.configDic)


    def insert_log(self, text):
        self.consoleWidget.normalOutputWritten(text)

    def started_scrapy(self):
        self.is_collect = True
        self.consoleWidget.normalOutputWarp("开始采集")

    def finished_scrapy(self, spider):
        self.is_collect = False
        self.consoleWidget.normalOutputWarp("结束采集")

        if self.collect_type == "common" and spider == "txv":
            self.scrapy_worker.run("txcreator")
        
        self.collect_type = ""
